import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/demo/route/unkown_page.dart';

import 'about.dart';

/// 1. 创建全局 eventBus 对象
final eventBus = EventBus();

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: MyBody.routeName,
      routes: {
        MyBody.routeName: (context) => MyBody(),
        AboutPage.routeName: (context) => AboutPage(),
      },
      onGenerateRoute: (settings) {
        // 这里可以做一些在页面跳转前的操作
        if (settings.name == "XXXX") {
          return null;
        }
      },
      onUnknownRoute: (settings) => MaterialPageRoute(builder: (context) => UnknownPage()),
    );
  }
}

class MyBody extends StatefulWidget {
  static const String routeName = "/";

  MyBody({Key? key}) : super(key: key);

  @override
  State<MyBody> createState() => _MyBodyState();
}

class _MyBodyState extends State<MyBody> {
  int counter = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("RouteName")),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(
                onPressed: () {
                  Future res =
                      Navigator.of(context).pushNamed(AboutPage.routeName, arguments: counter + 1);
                  res.then((value) => setState(() {
                        counter = value!;
                      }));
                },
                child: Text(
                  "跳转到关于 当前计数 $counter",
                  style: TextStyle(fontSize: 20),
                ),
              ),
              ElevatedButton(
                  onPressed: () => Navigator.of(context).pushNamed("dfw"),
                  child: Text("错误跳转", style: TextStyle(fontSize: 20)))
            ],
          ),
        ));
  }
}
